import { createRouter, createWebHistory } from 'vue-router';
import{getInfo} from '@/api/Login';


const routes = [
  {
    path: '/',
    redirect: 'Home'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false}
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/warehouse',
    name: 'Warehouse',
    component: () => import('../views/Warehouse.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/card',
    name: 'Card',
    component: () => import('../views/Card.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../views/Admin.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/cardmate',
    name: 'Cardmate',
    component: () => import('../views/Cardmate.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/users',
    name: 'Users',
    component: () => import('../views/Users.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/log',
    name: 'Log',
    component: () => import('../views/Log.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/carddetailpage/:id',
    name: 'CardDetailPage',
    component:  () => import('../views/CardDetailPage.vue'),
    props: true
  }
]


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if(to.name === 'Login'){
    console.log('进行登录');
    next()  
  }
  else{
    getInfo().then(res => {
    if(res.data.code === 200){
      console.log('登录成功');
      next()
    }
    else{
      console.log('未登录登录');
      next({name: 'Login'})
    }
  })
  }
})

export default router